package com.xywy.bloodandroid.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class MyViewGroup extends ViewGroup {
	int mHorizontalSpacing = 10;
	int mVerticalSpacing = 10;
	int width;
	int height;

	public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public MyViewGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyViewGroup(Context context) {
		super(context);
	}

	@Override
	protected void onLayout(boolean arg0, int l, int t, int r, int b) {
		width = getMeasuredWidth();
		int temp_height = getChildAt(0).getMeasuredHeight();
		int sum_height = temp_height;
		int sum_width = 0;
		int change = 0;
		for (int i = 0; i < getChildCount(); i++) {
			View tv =  getChildAt(i);
			int temp_width = tv.getMeasuredWidth();
			// �Ժ�Ķ��ü��� ���
			int last_width = sum_width+mHorizontalSpacing;
			sum_width = (int) (sum_width + temp_width + mHorizontalSpacing);
			if (sum_width < width) {
				tv.layout(last_width, sum_height - temp_height, sum_width,
						sum_height);
			} else {
				change = 0;
				sum_width = 0;
				sum_height = sum_height + temp_height + mVerticalSpacing;
				tv.layout(0, sum_height - temp_height, temp_width, sum_height);
				sum_width = (int) (sum_width + temp_width);
			}
			// if()
		}
	}


	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int measureWidth = measureWidth(widthMeasureSpec);
		int measureHeight = measureHeight(heightMeasureSpec);
		// �����Զ����ViewGroup�������ӿؼ��Ĵ�С
		measureChildren(widthMeasureSpec, heightMeasureSpec);
		// �����Զ���Ŀؼ�MyViewGroup�Ĵ�С
		setMeasuredDimension(measureWidth, measureHeight);
	}

	private int measureWidth(int pWidthMeasureSpec) {
		int result = 0;
		int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);// �õ�ģʽ
		int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);// �õ��ߴ�

		switch (widthMode) {
		/**
		 * mode�����������ȡֵ�ֱ�ΪMeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY,
		 * MeasureSpec.AT_MOST��
		 * 
		 * 
		 * MeasureSpec.EXACTLY�Ǿ�ȷ�ߴ磬
		 * �����ǽ��ؼ���layout_width��layout_heightָ��Ϊ������ֵʱ��andorid
		 * :layout_width="50dip"������ΪFILL_PARENT�ǣ����ǿؼ���С�Ѿ�ȷ���������Ǿ�ȷ�ߴ硣
		 * 
		 * 
		 * MeasureSpec.AT_MOST�����ߴ磬
		 * ���ؼ���layout_width��layout_heightָ��ΪWRAP_CONTENTʱ
		 * ���ؼ���Сһ�����ſؼ����ӿռ�����ݽ��б仯����ʱ�ؼ��ߴ�ֻҪ������ؼ���������ߴ缴��
		 * ����ˣ���ʱ��mode��AT_MOST��size����˸��ؼ���������ߴ硣
		 * 
		 * 
		 * MeasureSpec.UNSPECIFIED��δָ���ߴ磬�������࣬һ�㶼�Ǹ��ؼ���AdapterView��
		 * ͨ��measure���������ģʽ��
		 */
		case MeasureSpec.AT_MOST:
		case MeasureSpec.EXACTLY:
			result = widthSize;
			break;
		}
		return result;
	}

	private int measureHeight(int pHeightMeasureSpec) {
		int result = 0;

		int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);
		int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);

		switch (heightMode) {
		case MeasureSpec.AT_MOST:
		case MeasureSpec.EXACTLY:
			result = heightSize;
			break;
		}
		return result;
	}

}
